﻿
using Microsoft.Extensions.Logging;
using SHKF.B2C.BLL;
using SHKF.B2C.YouZanYun.Model.Entity.Order;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace SHKF.B2C.YouZanYun.BLL
{ 
    /// <summary>
    /// 有赞云数据库操作
    /// </summary>
    public class YZYOrderBLL : BaseBll
    {
       
        public YZYOrderBLL(SqlSugarClient db ):base(db)
        {
           // this.platform = PlatformEnum.YouZanYun;
           
            this.db.ChangeDatabase("PtE_YZY_LM");

        }

        /// <summary>
        /// 保存有赞的数据
        /// </summary>
        /// <param name="order"></param>
        //public void SaveAll(OrderDto order, OrderReceiverEntity orderReceiver, OrderMsg orderMsg)
        //{
        //    // 定单 
        //    OrderContentEntity orderEntity = CreateOrder(order,orderMsg); 
        //    List<OrderDetailEntity> orderDetails = CreateOrderDetail(orderMsg, orderEntity.order_id, orderEntity.app_poi_code);
             
        //    try
        //    {
        //        this.freeSql = base.GetLMDAL();
        //        this.freeSql.Transaction(() => {

        //            //保存定单
        //            //this.SaveOrder(orderEntity);

        //            ////保存收货人
        //            //orderReceiver.FKSN = orderEntity.SN;
        //            //this.SaveReceiver(orderReceiver);

        //            ////保存明细
        //            //if (orderDetails != null)
        //            //{
        //            //    foreach (OrderDetailEntity orderDetail in orderDetails)
        //            //    {
        //            //        orderDetail.FKSN = orderEntity.SN;

        //            //        this.SaveDetail(orderDetail);
        //            //    }
        //            //}
        //        });
        //    }
        //    catch (Exception ex)
        //    {
        //        this.logger.LogWarning($"msg:{ex.Message}");
        //    }

        //}

        public int SaveAll(TradeContentEntity order, List<TradeDetailEntity> details)
        {

            try
            {
                order.pDate = DateTime.Now;

                bool bExist = this.db
                    .Queryable<TradeContentEntity>()
                    .Where(ww=>ww.id == order.id)
                    .Any();
                if (!bExist)
                {
                    try
                    {
                        this.db.BeginTran();

                        //保存
                        long SN = this.db
                        .Insertable<TradeContentEntity>(order)
                        .ExecuteReturnBigIdentity(); ;

                        //保存明细
                        foreach (TradeDetailEntity detail in details)
                        {
                            detail.FKSN = SN;

                            this.db
                            .Insertable<TradeDetailEntity>(detail)
                            .ExecuteCommand();
                        }

                        this.db.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        this.db.RollbackTran();
                        throw;
                    } 
                }
                else
                {
                    order.SN = this.db.Queryable<TradeContentEntity>().Select(it=>it.SN).First();
                    return 2;
                }
                return 0;
            }
            catch (Exception ex)
            {
                //base.WriteLogInformation("YZYOrderBLL->SaveAll", ex.Message);
             
                throw ex;
            }
        }

        private int GetStatus(string status)
        {
            int result = 0;
            if (status == "WAIT_BUYER_PAY") //交易创建
            {
                result = 1;
            }
            else if(status == "TRADE_PAID" || status == "WAIT_SELLER_SEND_GOODS") //交易支付 + 买家付款(即商家待发货)
            {
                result = 4;
            }
            return result;
        }

        private long GetID(string id)
        {
            int length = id.Length;
            return long.Parse( id.Substring(length - 19) );
        }

        //private OrderContentEntity CreateOrder(OrderDto order, OrderMsg orderMsg)
        //{ 
        //    Address_Info address_info = orderMsg.full_order_info.address_info;
        //    Order_Info order_info = orderMsg.full_order_info.order_info;
        //    Pay_Info pay_info = orderMsg.full_order_info.pay_info;
        //    Remark_Info remark_Info = orderMsg.full_order_info.remark_info;

        //    OrderContentEntity orderEntity = new OrderContentEntity();

        //    orderEntity.ID = order.id;
        //    orderEntity.app_poi_code = order.kdt_id;
        //    orderEntity.status = GetStatus( order.status ); 
        //    orderEntity.order_id = GetID( order.id );
        //    orderEntity.wm_order_id_view = orderEntity.order_id;  
        //    orderEntity.wm_poi_name = order.kdt_name;
        //    orderEntity.wm_poi_address = "";
        //    orderEntity.wm_poi_phone = "";

        //    orderEntity.recipient_address = address_info.delivery_address;
        //    orderEntity.recipient_phone = address_info.receiver_tel;
        //    orderEntity.recipient_name = address_info.receiver_name;
        //    orderEntity.shipping_fee = 0;

        //    orderEntity.total = pay_info.payment;

        //    orderEntity.original_price = pay_info.total_fee;
        //    orderEntity.caution = remark_Info.buyer_message;

        //    orderEntity.shipper_phone = "";
        //    orderEntity.has_invoiced = 0;
        //    orderEntity.invoice_title = "";
        //    orderEntity.Platform = PlatformEnum.YouZanYun;


        //    orderEntity.ctime = order_info.created;
        //    orderEntity.utime = order_info.update_time;
        //    orderEntity.delivery_time = address_info.delivery_end_time;

        //    orderEntity.day_seq = 0;
        //    orderEntity.total_weight = 0;

        //    return orderEntity; 
        //}
  
        //private List<OrderDetailEntity> CreateOrderDetail(OrderMsg msg, long order_id, long app_poi_code)
        //{
        //    List<OrderDetailEntity> list = new List<OrderDetailEntity>();

        //    foreach (Orders orderDetailPush in msg.full_order_info.orders)
        //    {
        //        OrderDetailEntity orderDetailEntity = new OrderDetailEntity();

        //        orderDetailEntity.FKSN = order_id;
        //        orderDetailEntity.actual_price = orderDetailPush.total_fee / orderDetailPush.num;
        //        orderDetailEntity.original_price = orderDetailPush.price;
        //        orderDetailEntity.app_medicine_code = orderDetailPush.item_id.ToString();
        //        orderDetailEntity.product_name = orderDetailPush.title;
        //        orderDetailEntity.upc = orderDetailPush.item_barcode;
        //        orderDetailEntity.qty = orderDetailPush.num;
        //        orderDetailEntity.price = orderDetailPush.price;
        //        orderDetailEntity.box_num = 0;
        //        orderDetailEntity.box_price = 0;
        //        orderDetailEntity.unit = "";
        //        orderDetailEntity.food_discount = 1;
        //        orderDetailEntity.weight = 0;
        //        orderDetailEntity.weight_unit = "";
               
        //        //erp的productsn,暂时没转
        //        orderDetailEntity.ProductSN = orderDetailPush.item_id;

        //        list.Add(orderDetailEntity);
        //    }

        //    return list;
        //}


    }
}
